package edu.csl.study.spark.basic

import java.io.File
import java.net.URI

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}

object FileTools {

  def deleteHDFSDir(path:String) = {
    val conf:Configuration = new Configuration();
    val  fileSystem:FileSystem = FileSystem.get(URI.create(path), conf);
    //生产中慎用
    if (fileSystem.exists(new Path(path))) {
      fileSystem.delete(new Path(path), true);
    }
  }
  def deleteDir(path:File):Boolean = {
    if (!path.exists)  return  true
    if (path.isFile) {
      return path.delete
    }
    val files = path.listFiles
    for (i <- 0 until files.length) {
      deleteDir(files(i))
    }
    return path.delete
  }
  def deleteDir(path:String):Boolean = {
    var file = new File(path)
    if (!file.exists)  return  true
    if (file.isFile) {
      return file.delete
    }
    val files = file.listFiles
    for (i <- 0 until files.length) {
      deleteDir(files(i))
    }
    return file.delete
  }
}
